Skip to content

fix(gtm): invoke onBeforeGtmStart callback when ID is in config#584

Merged
harlan-zw merged 4 commits intomainfrom
gtm-config-callback
Jan 18, 2026
Merged

fix(gtm): invoke onBeforeGtmStart callback when ID is in config#584
harlan-zw merged 4 commits intomainfrom
gtm-config-callback

Conversation

@harlan-zw
Copy link
Collaborator

@harlan-zw harlan-zw commented Jan 18, 2026

🔗 Linked issue

Resolves #522

❓ Type of change

  • 📖 Documentation (updates to the documentation or readme)
  • 🐞 Bug fix (a non-breaking change that fixes an issue)
  • 👌 Enhancement (improving an existing functionality)
  • ✨ New feature (a non-breaking change that adds functionality)
  • 🧹 Chore (updates to the build process or auxiliary tools and libraries)
  • ⚠️ Breaking change (fix or feature that would cause existing functionality to change)

📚 Description

When GTM ID is configured in nuxt.config.scripts.registry, the module generates a plugin that pre-initializes the script at app startup—before any user components mount. This meant onBeforeGtmStart callbacks passed later in components were never invoked since clientInit had already run with no callback in its closure.

Now after useRegistryScript returns, we check if window.gtag already exists (indicating pre-initialization) and immediately invoke the callback.

// nuxt.config - ID in config
scripts: {
  registry: {
    googleTagManager: { id: "GTM-XXXXXX" }
  }
}

// component - callback now works
useScriptGoogleTagManager({
  onBeforeGtmStart: (gtag) => {
    gtag('consent', 'default', { ad_storage: 'denied' })
  }
})

harlan-zw and others added 4 commits January 16, 2026 01:58
Combined commits:
- fix(plausible): use consistent window reference in clientInit stub
When GTM ID is configured in nuxt.config, the plugin pre-initializes
the script before user components mount. This caused onBeforeGtmStart
callbacks passed to useScriptGoogleTagManager() to never be invoked.

Now checks if script was already initialized (gtag exists) and invokes
the callback immediately for cached/pre-initialized scripts.

Closes #522

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
@vercel
Copy link
Contributor

vercel bot commented Jan 18, 2026

The latest updates on your projects. Learn more about Vercel for GitHub.

Project Deployment Review Updated (UTC)
scripts-docs Ready Ready Preview, Comment Jan 18, 2026 2:04am
scripts-playground Ready Ready Preview, Comment Jan 18, 2026 2:04am

@pkg-pr-new
Copy link

pkg-pr-new bot commented Jan 18, 2026

Open in StackBlitz

npm i https://pkg.pr.new/nuxt/scripts/@nuxt/scripts@584

commit: 46f2e17

@harlan-zw harlan-zw merged commit f8ce5a1 into main Jan 18, 2026
10 checks passed
@harlan-zw harlan-zw deleted the gtm-config-callback branch January 18, 2026 02:05
@harlan-zw harlan-zw mentioned this pull request Jan 23, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

useScriptGoogleTagManager onBeforeGtmStart callback not triggered when GTM ID is set via nuxt.config

1 participant